###########################################
# Recoding No Vote 
###########################################

# Clear workspace
rm(list = ls())

# Load packages 
library(lmtest)
library(sandwich)
library(cregg)        
library(dplyr)        
library(ggplot2)      
library(gridExtra)    
library(forcats)      
library(haven)        
library(scales)       

# Load data
load("anticorr_replication_data.RData")

# Recode vote 
table(d$q1)
d$outcome_q1_v2 <- ifelse(d$q1 == d$candidato, 1, 0)
table(d$outcome_q1_v2)

################################
# Figure A5
################################

amces1 <- cj(d, outcome_q1_v2 ~ anticorruption + gender + ideology + party + bribery + embezzlement + competence + age + education, id = ~id)

plot1 <- plot(amces1[1:3, ], text.size = 17, xlab = "Change in Electoral Support") +
  theme_classic() +
  ggtitle("") +
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "none",
    panel.border = element_rect(linetype = "solid", color = "black", fill = NA),
    panel.background = element_rect(fill = "white"),
    axis.line = element_blank()
  ) +
  scale_color_manual(values = rep("black", 9))

ggsave("figure1_zeroes.pdf", plot1, width = 7, height = 3)

#########################################
# Figure A6
#########################################

# AMCE by bribery
amces4 <- cj(d, outcome_q1_v2 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "amce", by = ~bribery)
amces4 <- rbind(amces4[1:3, ], amces4[21:23, ])

plot4 <- plot(amces4, group = "bribery", vline = 0.0) +
  theme(legend.title = element_blank()) +
  scale_colour_manual(values = c("black", "grey"), na.translate = FALSE)

# Marginal Means
mms4 <- cj(d, outcome_q1_v2 ~ anticorruption + gender + ideology + party + embezzlement + competence + age + education, id = ~id, estimate = "mm", by = ~bribery)
mms4 <- rbind(mms4[1:3, ], mms4[21:23, ])

p4 <- plot(mms4, group = "bribery") +
  theme(legend.title = element_blank()) +
  scale_colour_manual(values = c("black", "grey"), na.translate = FALSE)

# Combine Plots
f4 <- grid.arrange(plot4, p4, ncol = 2)
ggsave("figure4_zeroes.pdf", f4, width = 9, height = 3)

#########################################
# Figure A7
#########################################

# AMCE by gender
amces6 <- cj(d, outcome_q1_v2 ~ anticorruption + ideology + party + bribery + embezzlement + competence + age + education, id = ~id, estimate = "amce", by = ~gender)
amces6 <- rbind(amces6[1:3, ], amces6[21:23, ])

plot6 <- plot(amces6, group = "gender", vline = 0.0) +
  theme(legend.title = element_blank()) +
  scale_colour_manual(values = c("black", "grey"), na.translate = FALSE)

# Marginal Means
mms6 <- cj(d, outcome_q1_v2 ~ anticorruption + ideology + party +
             bribery + embezzlement + competence + age + education, id = ~id,
           estimate = "mm", by = ~gender)
mms6 <- rbind(mms6[1:3, ], mms6[21:23, ])

p6 <- plot(mms6, group = "gender", vline = 0.5) +
  theme(legend.title = element_blank()) +
  scale_colour_manual(values = c("black", "grey"), na.translate = FALSE)

# Combine Plots
f6 <- grid.arrange(plot6, p6, ncol = 2)
ggsave("figure6_zeroes.pdf", f6, width = 9, height = 3)
